#include "MyDbClass.h"
#include <QtSql/QSqlDatabase>
#include <QDebug>
#include <QTextCodec>
#include <QtSql/QSqlError>
#include <QtSql/QSqlDriver>
#include "globalData.h"

MyDbClassSqlite::MySqlDbHelp_sqlite* MyDbClassSqlite::MySqlDbHelp_sqlite::dbSelf = NULL;//静态成员变量必须初始化

MyDbClassSqlite::MySqlDbHelp_sqlite::MySqlDbHelp_sqlite()
{
    isOpen = false;
}
MyDbClassSqlite::MySqlDbHelp_sqlite::~MySqlDbHelp_sqlite()
{
    ;
}

MyDbClassSqlite::MySqlDbHelp_sqlite* MyDbClassSqlite::MySqlDbHelp_sqlite::getInstance()
{
    if (MyDbClassSqlite::MySqlDbHelp_sqlite::dbSelf == NULL)
    {
        MyDbClassSqlite::MySqlDbHelp_sqlite::dbSelf = new MySqlDbHelp_sqlite();
    }
    return MyDbClassSqlite::MySqlDbHelp_sqlite::dbSelf;

}
QSqlQuery MyDbClassSqlite::MySqlDbHelp_sqlite::getDataList(QString strSql, bool isNet)
{
    if (isOpen)
    {
        db.close();
        isOpen = false;
    }
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");//解决数据库查询的中文乱码问题
    QTextCodec::setCodecForLocale(codec);

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("DB/SelfDb.db");
    if (!db.open())
    {
        isOpen = false;
        qDebug() << "Error: Failed to connect database." << db.lastError();
    }
    else
    {
        isOpen = true;
        //qDebug() << "Succeed to connect database.";
    }

    QSqlQuery query1(db);
    query1.prepare(strSql);
    query1.exec();

    codec = QTextCodec::codecForName("GBK");//解决界面显示乱码问题
    QTextCodec::setCodecForLocale(codec);

    return query1;
}
bool MyDbClassSqlite::MySqlDbHelp_sqlite::excuteSql(QString sql, bool isNet)
{
    if (isOpen)
    {
        db.close();
        isOpen = false;
    }
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");//解决数据库查询的中文乱码问题
    QTextCodec::setCodecForLocale(codec);

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("DB/SelfDb.db");
    if (!db.open())
    {
        isOpen = false;
        qDebug() << "Error: Failed to connect database." << db.lastError();
    }
    else
    {
        isOpen = true;
        //qDebug() << "Succeed to connect database.";
    }

    QSqlQuery query(db);
    query.prepare(sql);
    bool result = query.exec();

    //db.close();

    codec = QTextCodec::codecForName("GBK");//解决界面显示乱码问题
    QTextCodec::setCodecForLocale(codec);
    return result;
}

void MyDbClassSqlite::MySqlDbHelp_sqlite::closeDb()
{
    db.close();
}

QSqlQuery MyDbClassSqlite::MySqlDbHelp_sqlite::getDataList(QString strSql, int& count)
{
    count = 0;
    if (isOpen)
    {
        db.close();
        isOpen = false;
    }
    QTextCodec *codec = QTextCodec::codecForName("UTF-8");//解决数据库查询的中文乱码问题
    QTextCodec::setCodecForLocale(codec);

    db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("DB/SelfDb.db");
    if (!db.open())
    {
        isOpen = false;
        qDebug() << "Error: Failed to connect database." << db.lastError();
    }
    else
    {
        isOpen = true;
        //qDebug() << "Succeed to connect database.";
    }

    QSqlQuery query1(db);
    query1.prepare(strSql);
    query1.exec();

    while (query1.next())
    {
        count++;
    }

    query1.exec();

    codec = QTextCodec::codecForName("GBK");//解决界面显示乱码问题
    QTextCodec::setCodecForLocale(codec);

    return query1;
}